// pages/product/first/first.js
var app = getApp()
var WxParse = require('../../../wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */

  data: {
    info: {
      product: {
        id: '',
        price: '',
        name: '',
        main_img: '',
        time: '',
        unit: '',
        service_category_id: '',
        content: ''
      },
      address: {
        id: '',
        name: '',
        phone: '',
        address: '',
        province_id: '',
        city_id: '',
        district_id: ''
      },
      time: {
        start_at: '',
        end_at: '',
        hours: 1
      },
      cleaner: {
        id: '',
        wx_avatar_url: '',
        real_name: ''
      },
      coupon: {
        id: '',
        price: '',
      },
      remark: '',
      totalPrice: 0,
      textarea: true
    },
    selUnit: 0,
    maskShow: false,
    skuNum: 0,
    skuList: [
      { id: "1", value: "160" },
      { id: "2", value: "170" },
      { id: "3", value: "180" },
      { id: "4", value: "190" },
      { id: "5", value: "200" },
      { id: "6", value: "210" },
      { id: "7", value: "220" },
      { id: "8", value: "230" }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      info: JSON.parse(options.info)
    })
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initApp()
  },

  /**
   * initApp
   */
  initApp: function () {
    let that = this
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/product/detail?access_token=' + accessToken + '&id=' + that.data.info.product.id,
        success: res => {
          if (res.data.status === 1) {
            WxParse.wxParse('content', 'html', res.data.data.detail.content, that, 5);
            that.setData({
              'info.product': res.data.data.detail
            })
            wx.hideLoading()
          }
        }
      })
    })
  },

  confirmOrder: function () {
    let that = this
    delete that.data.info.product.content
    wx.redirectTo({
      url: '/pages/confirm/confirm?info=' + JSON.stringify(that.data.info),
    })
  },

  closeMask: function () {
    this.setData({
      maskShow: false
    })
  },

  showSku: function () {
    this.setData({
      maskShow: true
    })
  },

  selectSku: function (e) {
    this.setData({
      'info.time.hours': e.currentTarget.dataset.sku
    })
  }
})